/**
 * Currency Converter
 * SSUI Assignment 2
 * 
 * Team Members: Victor Rivera
 * 				 Tiffany Ng
 * 			     Justine Yang
 * 			     Syp Vandy
 */

package com.android.converter;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * This class is a database access helper which retrieves currency conversion values.
 *
 */
public class ConverterDbAdapter {
	public static final String KEY_CURRENCY = "currency";
    public static final String KEY_VALUE = "value";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "CurrencyDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    
    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
            "create table currencies (_id integer primary key autoincrement, "
                    + "currency text not null, value real not null);";
    
    
    private static final String DATABASE_NAME = "data";
    private static final String DATABASE_TABLE = "currencies";
    private static final int DATABASE_VERSION = 1;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(DATABASE_CREATE);
            intialData(db);
            
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
        
        public void intialData(SQLiteDatabase db){
        	String[] currencies = {"Euro","US Dollar","British Pound","Taiwanese New Dollar","Thai Baht","Canadian Dollar",
        			"Japanese Yen","Mexican Peso","Nepalese Rupee","Colombian Peso"};
        	double[] values = {1,0.704179,1.15395,0.0238060,0.0233241,0.726210,0.00851598,0.0591986,0.00988005,0.000375892};
        	
        	for (int i=0;i<currencies.length;i++){
        		ContentValues initialValues = new ContentValues();
                initialValues.put(KEY_CURRENCY, currencies[i]);
                initialValues.put(KEY_VALUE, values[i]);
                
                db.insert(DATABASE_TABLE, null, initialValues);
        	}
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public ConverterDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public ConverterDbAdapter open() throws SQLException {
    	mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }
    
    public void fillData(){
    	//this is for new values
    	String[] currencies = {};
    	double[] values = {};
    	
    	for (int i=0;i<currencies.length;i++){
    		ContentValues initialValues = new ContentValues();
            initialValues.put(KEY_CURRENCY, currencies[i]);
            initialValues.put(KEY_VALUE, values[i]);
            

            mDb.insert(DATABASE_TABLE, null, initialValues);
    	}
    	
    }
    
    public void close() {
        mDbHelper.close();
    }


    /**
     * Return a Cursor over the list of all notes in the database
     * 
     * @return Cursor over all notes
     */
    public Cursor fetchAllCurrencies() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_CURRENCY,
                KEY_VALUE}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the note that matches the given rowId
     * 
     * @param rowId id of note to retrieve
     * @return Cursor positioned to matching note, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchCurrency(long rowId) throws SQLException {

        Cursor mCursor =

                mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                        KEY_CURRENCY, KEY_VALUE}, KEY_ROWID + "=" + rowId, null,
                        null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }


}
